package main

import (
	"encoding/json"
	"net/http"

	"git.oschina.net/blueszhang/stock_be/session"
	log "github.com/Sirupsen/logrus"
	"github.com/gorilla/mux"
)

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/session", CreateSession).Methods("POST")
	http.Handle("/", r)
	http.ListenAndServe(":8080", nil)
}

func CreateSession(w http.ResponseWriter, r *http.Request) {
	s := session.NewSession()
	s.Put("code", "abc")
	jsonWriter := json.NewEncoder(w)
	err := jsonWriter.Encode(s)
	if err != nil {
		log.Errorln(err)
	}
}

func Register(w http.ResponseWriter, r *http.Request) {
	user := "root"
	passwd := "abc"
	phone := "13817902966"
	email := "blues.zhang.sh@gmail.com"
	sid := "925d1251-e9f8-4827-8e1d-bd9a8c75ef3a"
	code := "8392"
	if !ValidSession(sid, code) {

	}

}

func ValidSession(sid string, code string) bool {
	if s, ok := session.Get(sid); !ok {
		return false
	} else {
		if _code, ok := s.Get("code"); !ok {
			return false
		} else {
			codeStr := _code.(string)
			return codeStr == code
		}
	}
}
